package leetcodev1.字符串;

import java.util.HashSet;
import java.util.Set;

public class LeetCode3 {

    //滑动窗口
    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) {
            return 0;
        }
        int ret = 1, j = -1;//起点
        Set<Character> dict = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            while (dict.contains(s.charAt(i))) {
                j++;
                dict.remove(s.charAt(j));
            }
            dict.add(c);
            ret = Math.max(ret, i - j);
        }

        return ret;
    }
}
